package com.freeseed.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.freeseed.model.entity.*;
import com.freeseed.model.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("transcripts")
public class JSONTranscriptController extends ApiController {
    @Autowired
    private TranscriptMapper transcriptMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EnrollmentMapper enrollmentMapper;
    @Autowired
    private MajorDtlMapper majorDtlMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private AssessmentMapper assessmentMapper;

    /**
     * 输入学生的邮箱账号，查找该学生的所有成绩单
     * @param email
     * @return 返回的是一个key：String，value：Map<String,String>的map，用来存放json数据
     */
    @GetMapping("byStudent")
    public R getAllTranscriptByStudent(String email){
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_email",email));
        //判断是否是学生
        if(user.getRoleId() == 2){
            try{
                //需要return的HashMap
                Map<String,Map<String,String>> stringMapHashMap = new HashMap<>();
                List<Enrollment> enrollmentList = enrollmentMapper.selectList(new QueryWrapper<Enrollment>().eq("user_id",user.getUserId()));
                log.info(enrollmentList.toString());
                for(Enrollment enrollment:enrollmentList) {
                    MajorDtl majorDtl = majorDtlMapper.selectById(enrollment.getMajorDtlId());
                    log.info(majorDtl.toString());
                    List<Major> majorList = majorMapper.selectList(new QueryWrapper<Major>().eq("major_id",majorDtl.getMajorId()));
                    log.info(majorList.toString());
                    List<Assessment> assessmentList;
                    for (Major major : majorList) {
                        assessmentList = assessmentMapper.selectList(new QueryWrapper<Assessment>().eq("major_id", major.getMajorId()));
                        for (Assessment assessment : assessmentList) {
                            //限定enrollmentId和assessmentId，从数据库中选出transcript
                            Transcript transcript = transcriptMapper.selectOne(new QueryWrapper<Transcript>()
                                    .eq("enrollment_id", enrollment.getEnrollmentId())
                                    .eq("assessment_id", assessment.getAssessmentId()));
                            if(transcript != null) {
                                //把需要的信息从上面的对象中抽取出来，放入map中
                                Map<String,String> map = new HashMap<>();
                                map.put("Major Code",major.getMajorCode());
                                map.put("Major Name",major.getMajorName());
                                map.put("Year",String.valueOf(majorDtl.getSemesterYear()));
                                map.put("Semester",majorDtl.getSemester());
                                map.put("Assessment",assessment.getAssessmentName());
                                map.put("Mark",transcript.getMark());
                                map.put("Percentage",String.valueOf(assessment.getPercentage())+"%");
                                map.put("CreateTime",transcript.getCreateTime().toString());
                                //因为map的key会自动去重，所以用序号来代表key
                                int i = stringMapHashMap.size();
                                stringMapHashMap.put(String.valueOf(i + 1), map);
                            }
                        }
                    }
                }
                return success(stringMapHashMap);
            }catch (Exception e){
                return failed(e.getMessage());
            }
        }else {
            return failed("User is not a Student");
        }
    }
}
